package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;

import java.util.List;

/**
 * Created by YYY on 16/7/19.
 */
public class NeItemChooseAdapter extends RecyclerView.Adapter<NeItemChooseVH> implements View.OnClickListener{

    private Context context;
    private List<NeItem> neItems;
    private LayoutInflater layoutInflater;
    private NeItem currentNeItem;


    public NeItemChooseAdapter(Context context,List<NeItem> neItems)
    {
        this.context=context;
        this.neItems=neItems;
        layoutInflater=LayoutInflater.from(context);
    }


    @Override
    public NeItemChooseVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.activity_choose_ne_item_layout,null);
        return new NeItemChooseVH(itemView);
    }

    @Override
    public void onBindViewHolder(NeItemChooseVH holder, int position) {

        NeItem neItem=neItems.get(position);
        holder.neIdView.setText(neItem.getId());
        holder.neNameView.setText(TextUtils.isEmpty(neItem.getNeName())?"":neItem.getNeName());
        holder.neIpView.setText(TextUtils.isEmpty(neItem.getNeIp())?"":neItem.getNeIp());

        if (null!=currentNeItem&&currentNeItem.getId().equals(neItem.getId()))
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_checked);
        }else
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_uncheck);
        }

        holder.itemView.setTag(R.id.recyclerview_item_position,position);
        holder.itemView.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {

        if (null==neItems)
        {
            return 0;
        }else
        {
            return neItems.size();
        }

    }

    @Override
    public void onClick(View v) {

        int position= (int) v.getTag(R.id.recyclerview_item_position);
        if (null==currentNeItem)
        {
            currentNeItem=neItems.get(position);
        }else
        {
            if (neItems.get(position).getId().equals(currentNeItem.getId()))
            {
                currentNeItem=null;
            }else
            {
                currentNeItem=neItems.get(position);
            }
        }

        notifyDataSetChanged();

    }


    public void setNeItems(List<NeItem> neItems) {
        this.neItems = neItems;
        currentNeItem=null;
        notifyDataSetChanged();
    }

    public NeItem getCurrentNeItem() {
        return currentNeItem;
    }
}
